package com.engine.cn.base;

import org.springframework.util.CollectionUtils;

import java.util.*;

public class ListTest {
    public static List<String> removeIf(List<String> resource, String target) {
        if (CollectionUtils.isEmpty(resource)) {
            return Collections.emptyList();
        }

        resource.removeIf(r -> Objects.equals(target, r));
        return resource;
    }

    public static List<String> subLastFive(List<String> resource) {
        if (resource.size() <= 5) {
            return resource;
        }

        List<String> resultList = new ArrayList<>();
        for (int i = resource.size() - 5; i < resource.size(); i++) {
            resultList.add(resource.get(i));
        }

        return resultList;
    }

    public static LinkedList<String> pop(LinkedList<String> resource) {
        if (CollectionUtils.isEmpty(resource)) {
            return new LinkedList<>();
        }

        resource.pop();
        return resource;
    }

    public static List<String> skip(List<String> resource) {
        //subList与原列表共享数据，属于浅拷贝，拷贝的是对象的指向
        List<String> result2 = resource.subList(Math.max(0, resource.size() - 5), resource.size());
        //生成新的列表
        List<String> lastFive = resource.stream()
                //跳过前面的，取后面的 resource.size() - 5 个
                .skip(Math.max(0, resource.size() - 5))
                .toList();
        System.out.println(lastFive);
        return lastFive;
    }

    public static List<String> distinct(List<String> resource) {
        //list转set
        List<String> distinctList = new HashSet<>(resource).stream().toList();
        System.out.println(distinctList);
        //stream去重，优雅且保留顺序
        return resource.stream().distinct().toList();
    }

    public static void main(String[] args) {
        List<String> resource = new ArrayList<>();
        resource.add("老潘");
        resource.add("启超");
        resource.add("启超");
        resource.add("文杰");
        resource.add("张威");
        resource.add("文杰");
        resource.add("文杰");
        //去重
        List<String> distinctlist = distinct(resource);
        System.out.println(distinctlist);
    }
}
